package com.lin.springcloud.controller;

import com.lin.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Created by Lin Weihong
 * @date on 2022/4/3 20:18
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")  //全局的,如果没有指明fallbackMethod的，使用全局的，指明了的，用独有的

public class OrderHystrixController {
    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")

    public String paymentInfo_OK(@PathVariable("id") Integer id) {
//        int a = 10/0;
        String res = paymentHystrixService.paymentInfo_OK(id);
        return res;
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "paymentInfoTimeOutFallBackMethod", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")  //2秒钟以内就是正常的业务逻辑,
//
//    })
    @HystrixCommand //使用default的,因为这边只有一个服务降级，所以必须把上面的代码注释掉，否则会有冲突
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        int a = 10 / 0;
        String res = paymentHystrixService.paymentInfo_TimeOut(id);
        return res;
    }

    public String paymentInfoTimeOutFallBackMethod(Integer id) {
        return "我是消费者80，对方支付系统繁忙，请10秒钟后重试";
    }

    //下面是全局fallback方法
    public String payment_Global_FallbackMethod() {
        return "Global异常处理信息，请稍后再试,(┬＿┬)";
    }

}
